﻿
#include <iostream>
#include "../../xcom/xall.hpp"


class connector :public xevent
{
public:
    bool init()
    {
        bool ret = s_.tcp_create();
        X_CHECK_RET_BOOL(ret);

        ret = s_.set_nonblock(1);
        X_CHECK_RET_BOOL(ret);
        return true;
    }
    bool connect(const char *ip, port_t port)
    {
        return s_.connect(ip,port);
    }

    // EPOLLIN,EPOLLOUT
    virtual void handle_evt(uint32_t evt_type)
    {
        printf("handle_evt\n");
        if(s_.is_error())
        {
            printf("err\n");
            ee_->remove(this);
            
        }else{
            printf("ok\n");
            ee_->modify(EPOLLIN,this);
        }
        
    }
    virtual int32_t get_fd()
    {
        return s_.sock_;
    }

    xsock s_;
    xepoll *ee_ = nullptr;
};

int main(int argc, char *argv[])
{
    const char* ip = argv[1];
    port_t port = atoi(argv[2]);

    xepoll ee;
    ee.init();

    connector con;
    con.ee_ = &ee;
    con.init();
    con.connect(ip,port);
    ee.insert(EPOLLOUT,&con);
    while(1)
    {
        ee.wait();
    }


    return 0;
}


